package com.hjy.order.interceptor;

import com.hjy.auth.vo.MemberEntity;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberEntity> memberEntityThreadLocal = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser != null) {
            memberEntityThreadLocal.set((MemberEntity) loginUser);
        }else {
            response.sendRedirect("http://auth.gulimall.com/login.html");
        }
        return true;
    }
}
